#include <bits/stdc++.h>

#define in read()
#define fi first
#define se second
#define pb push_back
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

using pii = pair < int , int >;
using vec = vector < int >;
using veg = vector < pii >;
using ll = long long;
using db = double;

int read() {
    int x = 0; bool f = 0; char ch = getchar(); while(!isdigit(ch)) f |= ch == '-',ch = getchar();
    while(isdigit(ch)) x = x * 10 + (ch ^ 48),ch = getchar(); return f ? -x : x;
}

const int N = 1e5 + 10;
const int inf = 0x3f3f3f3f;

int a[N],n,stk[N],top,dp[N];

void solve() {
	n = in; rep(i, 1, n) a[i] = in;
	dp[n] = 0; stk[top = 1] = n;
	per(i, n - 1, 1) {
		dp[i] = inf;
		while(top && a[i] > a[stk[top]]) dp[i] = min(dp[i],dp[stk[top]] + 1),top--;
		if(top && a[i] == a[stk[top]]) dp[i] = min(dp[i],dp[stk[top]] + 1),dp[stk[top]] = min(dp[stk[top]],dp[i]);
		else stk[++top] = i;
	}
	if(dp[1] >= inf) puts("-1");
	else printf("%d\n",dp[1]);
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("1.in","r",stdin);
#endif
	for(int T = in; T; T--) solve();
}
